<!DOCTYPE html>
<html>
<head>
<title>角色权限分配</title>
<%include("/eova/meta.html"){}%>
<%include("/eova/include_base.html"){}%>
</head>
<body>
<style>
.extends, label {
	font-size: 15px;
}

input[type="checkbox"] {
	font-family: Tahoma;
	vertical-align: middle;
	height: 16px;
	width: 16px;
}

.extends {
	line-height: 20px;
	padding: 5px;
}
.layui-table {
	margin: 0;
}
.layui-table tr, .layui-table td {
	height: 20px !important;
	line-height: 20px !important;
	margin: 0;
	padding: 3px;
	cursor: pointer;
	user-select:none;
}
</style>
<body>
<div class="extends" title="自动继承选择角色的权限到当前角色(复制权限数据)">
	继承角色 ： <%for(x in roles){%> <label><input type="checkbox" name="extends" value="${x.id}" onclick="chooseFun(${x.id}, this);">${x.name}</label> <%}%>
</div>

<div style="padding-left: 5px;">
	<button class="layui-btn layui-btn-sm" onclick="checkAll()">全选</button>
	<button class="layui-btn layui-btn-sm" onclick="checkAllNo()">反选</button>
	<button class="layui-btn layui-btn-sm" onclick="checkKey('查询')">所有查询</button>
	<button class="layui-btn layui-btn-sm" onclick="checkKey('新增')">所有新增</button>
	<button class="layui-btn layui-btn-sm" onclick="checkKey('修改')">所有修改</button>
	<button class="layui-btn layui-btn-sm" onclick="checkKey('删除')">所有删除</button>
	<button class="layui-btn layui-btn-sm" onclick="checkKey('查看')">所有查看</button>
	<button class="layui-btn layui-btn-sm" onclick="checkKey('导入')">所有导入</button>
</div>

<table class="layui-table">
	<colgroup>
		<col width="180">
		<col>
	</colgroup>
	<thead>
		<tr>
			<th>菜单</th>
			<th>功能(双击行正/反选本行所有功能)</th>
		</tr>
	</thead>
	<tbody>
		<%var SP = "";%> <%for(m in menus){%>
		<tr>
			<td>
			<%
			// 手工撸树
			if(m.type == 'dir'){
				SP = SP + "&nbsp;";
			}
			if(m.parent_id == 0){
				SP = "";
			}
			if(m.type != 'dir'){
				SP = "&nbsp;&nbsp;◇";
			}
			%>
			${SP == '' ? '◢' : SP}${m.name}
			</td>
			<td>
			<%if(!isEmpty(m.btnname!)){%> 
			<%for(b in strutil.split(m.btnname, ",")){var bids = strutil.split(m.btnid, ",");var bid = bids[bLP.index-1];%>
				<label><input type="checkbox" id="${bid}" value="${bid}" name="funBtn" fun="${b}">${b}</label>&nbsp; <%}%> 
			<%}%>
			</td>
		</tr>
		<%}%>
	</tbody>
</table>

<script type="text/javascript" src="${CDN!STATIC!}/eova/ui/js/common.js?v=${VER!}"></script>
<script type="text/javascript" src="${CDN!STATIC!}/eova/ui/ext/eova.table.js?v=${VER!}"></script>
<script type="text/javascript" src="${CDN!STATIC!}/eova/ui/ext/eova.layer.js?v=${VER!}"></script>
<script type="text/javascript">
// 确认按钮回调
var btnSaveCallback = function($dialog, ID, $$) {
	var url = '/auth/roleChoose/' + ${rid};
	var o ={
			'checks': getCheckIds(),
			'extends': getExtendsIds()
	};
	$.post(url, o, function(result) {
		if (result.success) {
			$$.msg("操作成功！");
			$.dialogClose(parent.layer, window);
		} else {
			$$.error(result.msg);
		}
	}, 'json');
};
</script>
<script>

$(function(){
	// 功能加载完成之后加载已分配功能
	chooseFun(${rid});
});

// 选中指定角色已授权功能
function chooseFun(rid, ck){
	$.ajax({
        url: '/auth/getRoleFunJson/' + rid,
        dataType: 'json',
        success: function(data) {
            // 将返回数据转换为JSON对象
        	var list = eval(data);
        	var isCheck = true;
    		if(ck){isCheck = ck.checked;}
        	for(i = 0; i < list.length; i++){
        		var bid = list[i];
        		$('#'+ bid).prop('checked', isCheck);
        	}
        }
    });
}

// 全选
var checkAll = function(){
	$('.layui-table input:checkbox').each(function() {
		this.checked = true;
	});
};
// 反选
var checkAllNo = function(){
	$('.layui-table input:checkbox').each(function() {
		if (this.checked) {
			this.checked = false;
		} else {
		    this.checked = true;
		}
	});
};
// 选择目标所有
var checkKey = function(key){
	$('input:checkbox[fun="'+ key +'"]').each(function() {
		 this.checked = true;
	});
};
// 双击选择本行所有
$(".layui-table tr").dblclick(function() {
	var cks = $(this).find("input:checkbox");
	var status = true;
	var checkboxs = cks.each(function(i, o) {
		// 以第一个勾选状态作为参照
		if (i == 0 && this.checked) {
			status = false;
		}
		this.checked = status;
	});
});

// 获取所有选中ID
function getCheckIds(){
	// 获取选中和不确定节点
    var ids = '';
	$(".layui-table input:checked").each(function() {
		if (ids != '') ids += ',';
        ids += $(this).val();
	});
    return ids;
}

// 获取所有选中ID
function getExtendsIds(){
	// 获取选中和不确定节点
    var ids = '';
	$(".extends input:checked").each(function() {
		if (ids != '') ids += ',';
        ids += $(this).val();
	});
    return ids;
}

var btnSaveCallback = function($dialog, $widget, $$) {
	var url = "/auth/roleChoose/" + ${rid};
	var o ={
			'checks': getCheckIds(),
			'extends': getExtendsIds()
	};
	$.post(url, o, function(result) {
		if (result.success) {
			$$.msg("操作成功！");
			$.dialogClose(parent.layer, window);
		} else {
			$$.error(result.msg);
		}
	}, 'json');
};
</script>

</body>
</html>